#include "QDevTypeUnified.h"
#include <QApplication>

QString  			QDevTypeUnified::devLayoutType = "";
QString  			QDevTypeUnified::devType = "";
QString  			QDevTypeUnified::devImageDir = "";
T_imageFindLevel  	QDevTypeUnified::devImageFindLevel = IMAGE_FIND_LEVEL_NONE;
QString  			QDevTypeUnified::devIni = "";
double  			QDevTypeUnified::devFontSizeBase = -1.0;
// 已查找图片缓存
QList<QString> 		QDevTypeUnified::devImageList = QList<QString>();

// 获取布局类型
QString QDevTypeUnified::getLayoutTypeQString()
{
	if(!devLayoutType.isEmpty())
		return devLayoutType;
    QString _layout = QString(QString::fromLocal8Bit("MainLayout"));
	if(_layout == DEFAULT_LAYOUT_TYPE){
		devLayoutType = DEFAULT_LAYOUT_TYPE;
		qDebug() << devLayoutType;
		return devLayoutType;
	}
	devLayoutType = _layout + LATPUT_TYPE_SUFFIX;
	qDebug() << devLayoutType;
	return devLayoutType;
}
// 获取布局类型
T_widgetLayoutType QDevTypeUnified::getLayoutTypeInt()
{
	getLayoutTypeQString();
	char buf[2] = "A";
	for(int i  = 0; i < 26; i++){
		if(devLayoutType.indexOf(buf) != -1){
			qDebug() << i + 1;
			return (T_widgetLayoutType)(i + 1);
		}
		buf[0]++;
	}
	qDebug() << WIDGET_LAYOUT_NONE;
	return WIDGET_LAYOUT_NONE;
}
// 获取设备型号
QString QDevTypeUnified::getDevType()
{
	if(!devType.isEmpty())
		return devType;
    QString _devMode = QString(QString::fromLocal8Bit("windows"));
	_devMode = _devMode.toUpper();
	devType =  _devMode.mid(0,_devMode.indexOf("-"));
	qDebug() << devType;
	return devType;
}

// 获取配置文件
QString QDevTypeUnified::getDevIni()
{
	if(!devIni.isEmpty())
		return devIni;
	getLayoutTypeQString();
	getDevType();

	/////////////////// 先查找本地配置文件 ////////////////////////////
	QString iniDir = QString(SYSTEM_DIR_INI) +
			QString(devLayoutType) + QString("/");

	// 查找本地型号对应配置文件
	QString inifile = QString(devType) + ".ini";
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}

	// 查找本地型号对应默认配置文件
	iniDir = QString(SYSTEM_DIR_INI) +
			QString(devLayoutType) + QString("/");
	inifile = "default.ini";
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}

	// 查找本地默认配置文件
	iniDir = QString(SYSTEM_DIR_INI);
	inifile = DEFAULT_INI_PATH;
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}



	/////////////////// 后查找app资源中的配置文件 ////////////////////////////
	iniDir = QString(ROOT_DIR_INI) +
			QString(devLayoutType) + QString("/");

	// 查找型号对应配置文件
	inifile = QString(devType) + ".ini";
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}

	// 查找型号对应默认配置文件
	iniDir = QString(ROOT_DIR_INI) +
			QString(devLayoutType) + QString("/");
	inifile = "default.ini";
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}

	// 查找默认配置文件
	iniDir = QString(ROOT_DIR_INI);
	inifile = DEFAULT_INI_PATH;
	if(PathisExist(iniDir + inifile, PATH_TYPE_FILE)){
		devIni = iniDir + inifile;
		qDebug() << devIni;
		return devIni;
	}
	qDebug() << devIni;
	return devIni;

}

// 获取图片目录
QString QDevTypeUnified::getDevImageDir()
{
	if(!devImageDir.isEmpty())
		return devImageDir;
	getLayoutTypeQString();
	getDevType();

	/////////////////// 先查找本地图片目录 ////////////////////////////
	// 查找型号对应图片目录
	QString imageDir = QString(SYSTEM_DIR_IMAGE) +
				QString(devLayoutType) + QString("/") +
				QString(devType) + QString("/");
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LOCAL_LEVEL_0;
		qDebug() << devImageDir;
		return devImageDir;
	}

	// 查找型号对应默认图片目录
	imageDir = QString(SYSTEM_DIR_IMAGE) +
					QString(devLayoutType) + QString("/") +
					QString(DEFAULT_IMAGE_DIR);
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LOCAL_LEVEL_1;
		qDebug() << devImageDir;
		return devImageDir;
	}
	// 查找默认图片目录
	imageDir = QString(SYSTEM_DIR_IMAGE) +
					QString(DEFAULT_IMAGE_DIR);
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LOCAL_LEVEL_2;
		qDebug() << devImageDir;
		return devImageDir;
	}

	/////////////////// 后查找app资源图片目录 ////////////////////////////
	// 查找型号对应图片目录
	imageDir = QString(ROOT_DIR_IMAGE) +
				QString(devLayoutType) + QString("/") +
				QString(devType) + QString("/");
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LEVEL_0;
		qDebug() << devImageDir;
		return devImageDir;
	}

	// 查找型号对应默认图片目录
	imageDir = QString(ROOT_DIR_IMAGE) +
					QString(devLayoutType) + QString("/") +
					QString(DEFAULT_IMAGE_DIR);
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LEVEL_1;
		qDebug() << devImageDir;
		return devImageDir;
	}
	// 查找默认图片目录
	imageDir = QString(ROOT_DIR_IMAGE) +
					QString(DEFAULT_IMAGE_DIR);
	if(PathisExist(imageDir, PATH_TYPE_DIR)){
		devImageDir = imageDir;
		devImageFindLevel = IMAGE_FIND_LEVEL_2;
		qDebug() << devImageDir;
		return devImageDir;
	}
	devImageFindLevel = IMAGE_FIND_LEVEL_NONE;
	qDebug() << devImageDir;
	return devImageDir;
}

// 获取图片文件全路径
QString QDevTypeUnified::getImageFullPath(const QString &imgName)
{
	QString dir = getDevImageDir();
	QString path = "";
	////////////////// 先查找本地目录 /////////////////////
	if(devImageFindLevel >= IMAGE_FIND_LOCAL_LEVEL_0){
		path = dir + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}
	if(devImageFindLevel >= IMAGE_FIND_LOCAL_LEVEL_1){
		path = QString(SYSTEM_DIR_IMAGE) +
						QString(devLayoutType) + QString("/") +
						QString(DEFAULT_IMAGE_DIR) + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}
	if(devImageFindLevel >= IMAGE_FIND_LOCAL_LEVEL_2){
		path = QString(SYSTEM_DIR_IMAGE) +
						QString(DEFAULT_IMAGE_DIR) + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}

	////////////////// 再查找app资源目录 /////////////////////
	if(devImageFindLevel >= IMAGE_FIND_LEVEL_0){
		path = dir + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}
	if(devImageFindLevel >= IMAGE_FIND_LEVEL_1){
		path = QString(ROOT_DIR_IMAGE) +
						QString(devLayoutType) + QString("/") +
						QString(DEFAULT_IMAGE_DIR) + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}
	if(devImageFindLevel >= IMAGE_FIND_LEVEL_2){
		path = QString(ROOT_DIR_IMAGE) +
						QString(DEFAULT_IMAGE_DIR) + imgName;
		if(PathisExist(path, PATH_TYPE_FILE)){
			return path;
		}
	}
	qDebug() << "no find image:" << imgName;
	return "";

}

//判断目录或文件是否存在
bool QDevTypeUnified::PathisExist(QString path, T_pathType type)
{
	if(devImageList.contains(path)){
		return true;
	}
	QFileInfo info(path);
	bool ret = false;
	switch(type)
	{
		case PATH_TYPE_DIR:
		{
			if(info.isDir())
				ret = true;
			break;
		}
		case PATH_TYPE_FILE:
		{
			if(info.isFile())
				ret = true;
			break;
		}
		default:
			break;
	}
	if(ret){
		devImageList << path;
	}
	return ret;
}


// 获取字体大小基数
double QDevTypeUnified::getFontSizeBase()
{
	if(devFontSizeBase >= 0)
		return devFontSizeBase;
	QDesktopWidget* desktopWidget = QApplication::desktop();
	//获取设备屏幕大小
	QRect screenRect = desktopWidget->screenGeometry();
	unsigned int res = screenRect.width() * screenRect.height();


	if(res >= 120000 && res <= 140000){
		devFontSizeBase = 1.0;
	}else if(res >= 900000 && res <= 1100000){
		devFontSizeBase = 2.0;
	}else if(res >= 500000 && res <= 700000){
		devFontSizeBase = 1.5;
	} else if(res >= 300000 && res <= 400000){
		devFontSizeBase = 1.2;
	}
	else{
		devFontSizeBase = 1.0;
	}
	return devFontSizeBase;
}

// 获取字体大小 通过字体等级获取字体大小
int QDevTypeUnified::getFontSize(int sizeLevel)
{
	return getFontSizeBase() * sizeLevel;
}

// 获取图片类型
QString QDevTypeUnified::getImageType(QString image)
{

	if(image.indexOf("png"))
		return "png";
	else if(image.indexOf("jpg"))
		return "jpg";
	else if(image.indexOf("bmp"))
		return "bmp";
	return "";
}

